//
//  KKConsultVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/16.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKConsultVC.h"
#import "KKTopSelectView.h"
#import "KKMyHttp.h"
#import "KKApplyExpertProgressVC.h"
#import "KKConsultNoExpertCell.h"
#import "KKMyConsultM.h"
#import "KKDraftCell.h"
#import "KKAdviceChatVC.h"
#import "KKAdviceExpertChatVC.h"
#import "KKComHttp.h"
#import "KKUserInfoController.h"
#import "KKApplyExpertProtocolVC.h"

@interface KKConsultVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//0代表文章1代表视频
@property (nonatomic,assign) int flag;
@property (nonatomic,copy)NSString *page;
@property (nonatomic,copy)NSString *consultId;
@property(nonatomic,strong)NSArray *myConsultDatas;
@end

@implementation KKConsultVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
    if(self.flag==0){
        [self getMyAdvice];
    }else{
        [self getAdviceWaitAnswer];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的咨询";
    [self initView];
}

#pragma mark 初始化view
-(void)initView{
    self.flag=0;
    self.page=@"1";
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    KKTopSelectView *topItem=[[[NSBundle mainBundle] loadNibNamed:@"KKTopSelectView" owner:nil options:nil] lastObject];
    [topItem initData:@[@"我的咨询",@"咨询我的"]];
    topItem.selectBlock=^(int flag){
        self.flag=flag;
        [self.tableView reloadData];
        if(flag==1&&[self.expertFlag isEqualToString:@"0"]){
            [self showApplyExpertDialog];
            return;
        }
        if(flag==0){
            [self getMyAdvice];
        }else{
            [self getAdviceWaitAnswer];
        }
    };
    [self.topView addSubview:topItem];
    
    //取消group样式头部空白
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 0.1f)];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKDraftCell" bundle:nil] forCellReuseIdentifier:@"KKDraftCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKConsultNoExpertCell" bundle:nil] forCellReuseIdentifier:@"KKConsultNoExpertCell"];
}

#pragma mark 申请专家对话框
-(void)showApplyExpertDialog{
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"确定申请为专家吗" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self applyExpertProgress];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){//我的咨询
        KKDraftCell *consult=(KKDraftCell *)cell;
        [consult initConsultData:self.myConsultDatas[indexPath.section]];
    }else if(self.flag==1){//待回复
        if(![self.expertFlag isEqualToString:@"0"]){
            KKDraftCell *consult=(KKDraftCell *)cell;
            [consult initConsultExpertData:self.myConsultDatas[indexPath.section]];
        }
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(self.flag==1&&[self.expertFlag isEqualToString:@"0"]){
        return 1;
    }else{
        return self.myConsultDatas.count;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==1&&[self.expertFlag isEqualToString:@"0"]){
        return 220;
    }else{
        return 70;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){//我的咨询
        return [tableView dequeueReusableCellWithIdentifier:@"KKDraftCell"];
    }else if(self.flag==1){//待回复
        if([self.expertFlag isEqualToString:@"0"]){
            return [tableView dequeueReusableCellWithIdentifier:@"KKConsultNoExpertCell"];
        }else{
            return [tableView dequeueReusableCellWithIdentifier:@"KKDraftCell"];
        }
        
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKMyConsultM *consult=self.myConsultDatas[indexPath.section];
    if(self.flag==0){//我的咨询
        KKAdviceChatVC *chat=[[KKAdviceChatVC alloc] init];
        chat.consultId=consult.consultid;
        chat.flag=1;
        [self.navigationController pushViewController:chat animated:YES];
    }else if(self.flag==1){//待回复
        if(![self.expertFlag isEqualToString:@"0"]){
            KKAdviceExpertChatVC *expertChat=[[KKAdviceExpertChatVC alloc] init];
            expertChat.consultId=consult.consultid;
            expertChat.realName=consult.username;
            [self.navigationController pushViewController:expertChat animated:YES];
        }
    }
}

-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"删除"
                                                                       handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                           KKMyConsultM *consult=self.myConsultDatas[indexPath.section];
                                                                           self.consultId=consult.consultid;
                                                                           [self delConsult];
                                                                       }];
//    UITableViewRowAction *rowActionSec = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
//                                                                            title:@"追加评论"
//                                                                          handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//                                                                              NSLog(@"标记");
//                                                                          }];
//    rowActionSec.backgroundColor = [UIColor greenColor];
//    NSArray *arr = @[rowAction,rowActionSec];
    NSArray *arr = @[rowAction];
    return arr;
}

#pragma mark 接口--删除咨询
-(void)delConsult{
    [KKMyHttp delConsult:self.consultId success:^(id data) {
        if(self.flag==0){
            [self getMyAdvice];
        }else{
            [self getAdviceWaitAnswer];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark 接口--我的咨询
-(void)getMyAdvice{
    [self showDialog];
    [KKMyHttp getMyAdviceList:self.page success:^(id data) {
        self.myConsultDatas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--待回复
-(void)getAdviceWaitAnswer{
    [self showDialog];
    [KKMyHttp getAdviceWaitAnswer:self.page success:^(id data) {
        self.myConsultDatas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--申请专家进度
-(void)applyExpertProgress{
    [self showDialog];
    [KKComHttp applyExpertProgress:^(id data) {
        NSString *code=[data objectForKey:@"code"];
        if([code isEqualToString:@"ok"]){
            KKUserInfoController *user=[[KKUserInfoController alloc] init];
            user.flag=1;
            [self.navigationController pushViewController:user animated:YES];
        }else if ([code isEqualToString:@"list"]){
            KKApplyExpertProtocolVC *protocol=[[KKApplyExpertProtocolVC alloc] init];
            [self.navigationController pushViewController:protocol animated:YES];
        }else{
            NSString *msg=[data objectForKey:@"msg"];
            [self toast:msg];
        }
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
